class BlogsController < ApplicationController
  
  before_filter :login_required, :only => [:new, :edit, :create, :update, :destroy]
  before_filter :set_current_menu_and_title

  def index
    @blogs = Blog.paginate(:per_page => 10, :page => params[:page], :order => 'created_at desc')
    @rss_blogs = Blog.find(:all, :limit => 10, :order => 'created_at desc')
  end
  
  def show
    @blog = Blog.find(params[:id])
    @blog_comment = BlogComment.new
    @title = @blog.title
    @keywords = @blog.tag_list.join(' ')
    @description = @blog.content[0,100]
  end

  def new
    @blog = Blog.new
  end

  def edit
    @blog = Blog.find(params[:id])
    if @blog.user.id != current_user.id
      redirect_to "/not_found"
    end
  end

  def create
    @blog = Blog.new(params[:blog])
    @blog.user_id = current_user.id

    if @blog.save
      flash[:notice] = 'Blog was successfully created.'
      redirect_to(@blog)
    else
      render :action => "new"
    end
  end

  def update
    @blog = Blog.find(params[:id])
    @blog.user_id = current_user.id

    if @blog.update_attributes(params[:blog])
      flash[:notice] = 'Blog was successfully updated.'
      redirect_to(@blog)
    else
      render :action => "edit"
    end
  end

  def destroy
    @blog = Blog.find(params[:id])
    @blog.destroy

    redirect_to(blogs_url)
  end
  
  private
    def set_current_menu_and_title
      @current_menu = :blog
      @title = "博客"
    end
  
end
